package com.senyin.service.impl;

import java.util.Date;

import com.senyin.common.BaseResponse;
import com.senyin.common.ErrorCode;
import com.senyin.common.ResultUtils;
import com.senyin.dao.StaffInfoDao;
import com.senyin.entity.StaffInfo;
import com.senyin.service.StaffInfoService;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.http.HttpServletRequest;
import javax.xml.bind.annotation.XmlType;
import java.sql.SQLException;
import java.util.List;
import java.util.Locale;
import java.util.Objects;

import static com.senyin.constant.UserConstant.ADMIN_ROLE;
import static com.senyin.constant.UserConstant.DEFAULT_ROLE;

public class StaffInfoServiceImpl implements StaffInfoService {
    private StaffInfoDao staffInfoDao = new StaffInfoDao();

    @Override
    public BaseResponse login(String siId, String password, HttpServletRequest request,String captcha,String code) throws SQLException {
        if (!(captcha.toLowerCase(Locale.ROOT)).equals(code.toLowerCase(Locale.ROOT))){
            return ResultUtils.error(ErrorCode.PARAMS_ERROR,"验证码错误，请重新输入");
        }
        if (StringUtils.isAnyBlank(siId, password)) {
            return ResultUtils.error(ErrorCode.NULL_ERROR, "参数为空");
        }
        if (request == null) {
            return ResultUtils.error(ErrorCode.NULL_ERROR, "请求为空");
        }
        return staffInfoDao.login(siId, password, request);
    }

    @Override
    public BaseResponse<List<StaffInfo>> showAll(StaffInfo staffInfo) throws SQLException {
        if (staffInfo.getSiAuth()== ADMIN_ROLE){
            return staffInfoDao.showAll();
        }else {
            return ResultUtils.error(ErrorCode.NO_AUTH);
        }
    }

    @Override
    public BaseResponse addStaff(StaffInfo staffInfo,HttpServletRequest request) throws SQLException {
        if (Objects.isNull(staffInfo)) {
            return ResultUtils.error(ErrorCode.NULL_ERROR, "参数为空");
        }
        return staffInfoDao.addStaff(staffInfo,request);
    }

    @Override
    public BaseResponse modStaff(StaffInfo staffInfo, HttpServletRequest request) throws SQLException {
        if (Objects.isNull(staffInfo)) {
            return ResultUtils.error(ErrorCode.NULL_ERROR, "参数为空");
        }
        return staffInfoDao.modStaff(staffInfo,request);
    }

    @Override
    public BaseResponse delStaffByUUID(String siUUID, HttpServletRequest request) throws SQLException {
        if (StringUtils.isAnyBlank(siUUID)) {
            return ResultUtils.error(ErrorCode.NULL_ERROR, "参数为空");
        }
        return staffInfoDao.delStaffByUUID(siUUID,request);
    }
}
